<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BSBookableResource
 *
 * @author jianfeng
 */
class BSBookableResource extends BSEntity {

    //put your code here
    public $spaceId;
    public $appId;
    public $timeslotId;
    public $resourceId;
    public $amount;
    public $unitPrice;
    public $priceExpression;
    public $bookingOpenTime;
    public $bookingEndTime;
    public $repeatStartTime;
    public $repeatEndTime;
    public $priority;
    public $imageUrl;
    public $status;
    public $creatorId;

    public function getPropertyToColumnMapList($data = NULL) {
        $list = parent::getPropertyToColumnMapList($data);
        $list['amount'] = 'resource_amount';
        $list['priceExpression'] = 'price_expression';
        $list['bookingOpenTime'] = 'booking_open_time';
        $list['bookingEndTime'] = 'booking_end_time';
        $list['repeatStartTime'] = 'repeat_start_time';
        $list['repeatEndTime'] = 'repeat_end_time';
        $list['imageUrl'] = 'image_urls';
        return $list;
    }

    public function initBeforeLoad($data = NULL) {
        if ($data != 'notInitBeforeLoad') {
            parent::initBeforeLoad($data);
            self::initInput();
        }
    }

    public static function initInput($data = NULL) {

        $spaceId = Blyn::app()->getActiveSpace()->getId();
        $appId = Blyn::app()->getActiveApp()->getId();
        $creatorId = Blyn::app()->getCurrentUser()->getId();

        $r = new BSBookableResource('notInitBeforeLoad');
        $r->resourceId = 1;
        $r->spaceId = $spaceId;
        $r->appId = $appId;
        $r->creatorId = $creatorId;
        $r->unitPrice = 100;
        $r->timeslotId = 2;
        $r->save();
        BPermit::addPermit($r, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));

        $r = new BSBookableResource('notInitBeforeLoad');
        $r->resourceId = 2;
        $r->spaceId = $spaceId;
        $r->appId = $appId;
        $r->creatorId = $creatorId;
        $r->unitPrice = 100;
        $r->timeslotId = 1;
        $r->save();
        BPermit::addPermit($r, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));
    }
    
     public function save($context = null) {

        if ($this->spaceId <= 0)
            $this->spaceId = Blyn::app()->getActiveSpace()->getId();
        if ($this->appId <= 0)
            $this->appId = $appId = Blyn::app()->getActiveApp()->getId();
        if ($this->creatorId <= 0)
            $this->creatorId = $creatorId = Blyn::app()->getCurrentUser()->getId();
        if ($this->amount == 0)
            $this->amount = 1;

        parent::save($context);
    }

}
